<template>
  <div class="app-container home">
    <div class="home-item today">
      <h3 class="home-title">{{ $t('home.today.title') }}</h3>
      <n-spin :show="loading">
        <n-row class="today-body">
          <n-col>
            <!-- 计划单数 -->
            <div class="today-item">
              <div class="today-item-icon">
                <img src="@/assets/images/home1.png" alt="" srcset="" />
                <!-- <svg-icon icon-class="home-i1"/> -->
              </div>
              <div class="today-item-desc">
                <p class="num">{{ todayData.planTheSingular }}</p>
                <p>{{ $t('home.today.plannedOrders') }}</p>
              </div>
            </div>
          </n-col>
          <n-col>
            <!-- 完工单数 -->
            <div class="today-item">
              <div class="today-item-icon">
                <img src="@/assets/images/home2.png" alt="" srcset="" />
                <!-- <svg-icon icon-class="home-i2"/> -->
              </div>
              <div class="today-item-desc">
                <p class="num">{{ todayData.completionSingular }}</p>
                <p>{{ $t('home.today.completedOrders') }}</p>
              </div>
            </div>
          </n-col>
          <n-col>
            <!-- 剩余单数 -->
            <div class="today-item">
              <div class="today-item-icon">
                <img src="@/assets/images/home3.png" alt="" srcset="" />
                <!-- <svg-icon icon-class="home-i3"/> -->
              </div>
              <div class="today-item-desc">
                <p class="num" :class="todayData.remainingSingular >= 0 ? '' : 'numColor'">{{
                  todayData.remainingSingular >= 0
                    ? todayData.remainingSingular
                    : -todayData.remainingSingular
                }}</p>
                <p>{{
                  todayData.remainingSingular >= 0
                    ? $t('home.today.remainderOrders')
                    : $t('home.today.remainderOrdersr')
                }}</p>
              </div>
            </div>
          </n-col>
          <n-col>
            <!-- 计划产量 -->
            <div class="today-item">
              <div class="today-item-icon">
                <!-- <svg-icon icon-class="home-i4"/> -->
                <img src="@/assets/images/home4.png" alt="" srcset="" />
              </div>
              <div class="today-item-desc">
                <p class="num">{{ todayData.planProduction }}</p>
                <p>{{ $t('home.today.plannedProduction') }}</p>
              </div>
            </div>
          </n-col>
          <n-col>
            <!-- 实际产量 -->
            <div class="today-item">
              <div class="today-item-icon">
                <img src="@/assets/images/home5.png" alt="" srcset="" />
                <!-- <svg-icon icon-class="home-i5"/> -->
              </div>
              <div class="today-item-desc">
                <p class="num">{{ todayData.actualProduction }}</p>
                <p>{{ $t('home.today.completedProduction') }}</p>
              </div>
            </div>
          </n-col>
          <n-col>
            <!-- 剩余产量 -->
            <div class="today-item">
              <div class="today-item-icon">
                <img src="@/assets/images/home6.png" alt="" srcset="" />
                <!-- <svg-icon icon-class="home-i6"/> -->
              </div>
              <div class="today-item-desc">
                <p class="num" :class="todayData.remainingProduction >= 0 ? '' : 'numColor'">{{
                  todayData.remainingProduction >= 0
                    ? todayData.remainingProduction
                    : -todayData.remainingProduction
                }}</p>
                <p>{{
                  todayData.remainingProduction >= 0
                    ? $t('home.today.remainderProduction')
                    : $t('home.today.remainderProductionsr')
                }}</p>
              </div>
            </div>
          </n-col>
        </n-row>
      </n-spin>
    </div>
    <div class="home-item equipment">
      <h3 class="home-title">{{ $t('home.equipment.title') }}</h3>
      <n-spin :show="loading">
        <n-row class="equipment-body">
          <n-spin :show="!loadings">
            <div
              class="swiper-container"
              v-if="machineStateVoList && machineStateVoList.length > 0"
            >
              <div class="swiper-wrapper">
                <div
                  v-for="item in machineStateVoList"
                  :key="item.machineCode"
                  class="swiper-slide equipment-item"
                  :class="`equipment-item${item.machineStatus}`"
                >
                  <!-- <div class="equipment-item-status" :style="{ backgroundColor: getMachineStatusColor(item.machineStatus) }">
                {{ getMachineStatus(item.machineStatus) }}
              </div>
              <div class="equipment-item-title">
                {{ item.machineCode }}
              </div> -->
                  <div class="equipment-head">
                    <div class="equipment-head-i">
                      <DFSvgIcon
                        v-if="item.machineStatus == 1"
                        class="iconfont icon-yunhangicon"
                        :name="'yunhangicon'"
                      />
                      <DFSvgIcon
                        v-else-if="item.machineStatus == 2"
                        class="iconfont icon-tiaojiicon"
                        :name="'tiaojiicon'"
                      />
                      <DFSvgIcon v-else class="iconfont icon-tingjiicon" :name="'tingjiicon'" />
                      <span>{{ getMachineStatus(item.machineStatus) }}</span>
                    </div>
                    <div class="equipment-head-tit">
                      {{ item.machineCode }}
                    </div>
                    <!-- <div class="equipment-head-num" v-if="item.numMaintenance">
                  <el-tooltip class="item" effect="dark" :content="$t('home.equipment.tit')+' :  '+ item.numMaintenance" placement="top">
                    <i>{{item.numMaintenance}}</i>
                  </el-tooltip>
                </div> -->
                  </div>
                  <div class="equipment-item-desc">
                    <!-- 实际产量 -->
                    <p>
                      <span>{{ $t('home.equipment.actualOutput') }}</span>
                      <span>
                        {{ `${item.actualOutput || 0}` }}
                        <i>{{ $t('home.equipment.pcs') }}</i>
                      </span>
                    </p>
                    <!-- 平均速度  -->
                    <p>
                      <span>{{ $t('home.equipment.avgSpeed') }}</span>
                      <span>
                        {{ `${item.avgSpeed || 0}` }}
                        <i>{{ $t('home.equipment.speedUnit') }}</i>
                      </span>
                    </p>
                    <!-- 用电量  -->
                    <p>
                      <span>{{ $t('home.equipment.electricityConsumption') }}</span>
                      <span>
                        {{ `${item.electricityConsumption || 0}` }}
                        <i>{{ $t('home.equipment.deg') }}</i>
                      </span>
                    </p>
                    <!-- 用水量   -->
                    <p>
                      <span>{{ $t('home.equipment.waterConsumption') }}</span>
                      <span>
                        {{ `${item.waterConsumption || 0}` }}
                        <i>{{ $t('home.equipment.ton') }}</i>
                      </span>
                    </p>
                    <!-- 待保养数   -->
                    <p>
                      <span>{{ $t('home.equipment.tit') }}</span>
                      <span>
                        {{ `${item.numMaintenance || 0}` }}
                        <i>{{ $t('home.equipment.term') }}</i>
                      </span>
                    </p>
                  </div>
                </div>
              </div>
              <div v-show="machineStateVoList.length > slides * 2" class="swiper-pagination"></div>
            </div>
            <div class="swiper-container noeq" v-else>
              <DFSvgIcon class="empty-icon" name="noequipment" />
              <span class="empty-text">{{ $t('home.noEq') }}</span>
            </div>
          </n-spin>
        </n-row>
      </n-spin>
    </div>

    <div class="flex-center footer-view home-copyright">
      {{ $t('common.tipMsg.copyright') }}
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { ref, computed, onMounted, onBeforeUnmount, nextTick } from 'vue';
  import Swiper from 'swiper';
  // import { getHomeData, getMachine } from '@/api/home/index';
  import lang from './lang';
  import 'swiper/dist/css/swiper.min.css';
  import { useMixins } from '@/hooks/useMixins';
  // import data from './data';
  import { useProjectSettingStore } from '@/store/modules/projectSetting';
  import { onDutyProductionProgress } from '@render/api/production/order';

  const settingStore = useProjectSettingStore();

  const { loadLangFn, t } = useMixins();

  loadLangFn(lang);

  // Refs
  const initialSlide = ref(0);
  const maxSlide = ref(0);
  const firstSwiper = ref(false);
  const swiperNext = ref(true);
  const loading = ref(false);
  const loadings = ref(true);
  const swiper = ref(null);
  const timer = ref(null);
  const todayData = ref({
    planTheSingular: 0,
    completionSingular: 0,
    remainingSingular: 0,
    planProduction: 0,
    actualProduction: 0,
    remainingProduction: 0,
  });
  const machineStateVoList = ref([]);
  const flag = ref(true);
  const slides = ref(document.body.offsetWidth <= 1440 ? 3 : 4);

  // Computed
  const getMachineStatus = computed(() => (status) => {
    return status === 2
      ? `${t('home.equipment.adjust')}`
      : status === 1
      ? `${t('home.equipment.run')}`
      : `${t('home.equipment.stop')}`;
  });

  // const getMachineStatusColor = computed(() => (status) => {
  //   return status === '2'
  //     ? '#ffb843'
  //     : (status === '1' ? '#52c570' : '#a2a2a2');
  // });

  let time = null;

  // Methods
  const getData = async () => {
    try {
      const res = await onDutyProductionProgress();
      const {
        planTheSingular,
        planProduction,
        completionSingular,
        actualProduction,
        remainingSingular,
        remainingProduction,
        machineStateVoList: dataMachineStateVoList,
        // shiftError,
      } = res.data || {};

      todayData.value = {
        planTheSingular,
        planProduction,
        completionSingular,
        actualProduction,
        remainingSingular,
        remainingProduction,
      };

      // if (shiftError === '1') {
      //   message.error(t('home.tip'));
      //   getMachine(res);
      //   return;
      // }

      machineStateVoList.value = dataMachineStateVoList || [];

      const screenWidth = document.body.offsetWidth;
      if (
        (machineStateVoList.value.length > 8 && screenWidth > 1440) ||
        (machineStateVoList.value.length > 6 && screenWidth <= 1440)
      ) {
        const slide = machineStateVoList.value.length > 8 && screenWidth > 1440 ? 8 : 6;
        maxSlide.value = Math.floor(machineStateVoList.value.length / slide);
      }

      if (firstSwiper.value) {
        firstSwiper.value = false;
        if (swiper.value) swiper.value.destroy(true, true);
        loadings.value = false;
        time = setTimeout(() => {
          initSwiper();
          loadings.value = true;
        }, 300);
      }
    } catch (e) {
      console.log(e);
    }
  };

  // const getMachine = async (res:any) => {
  //   try {
  //     const res = res.data;
  //     machineStateVoList.value = res.machineStateVoList || [];

  //     if (firstSwiper.value) {
  //       firstSwiper.value = false;
  //       if (swiper.value) swiper.value.destroy(true, true);

  //       const screenWidth = document.body.offsetWidth;
  //       if (
  //         (machineStateVoList.value.length > 8 && screenWidth > 1440) ||
  //         (machineStateVoList.value.length > 6 && screenWidth <= 1440)
  //       ) {
  //         const slide = machineStateVoList.value.length > 8 && screenWidth > 1440 ? 8 : 6;
  //         maxSlide.value = Math.floor(machineStateVoList.value.length / slide);
  //         initSwiper();
  //       } else {
  //         initSwiper();
  //       }
  //     }
  //   } catch {
  //     // Handle error
  //   }
  // };

  // const slides = ref(null);

  const initSwiper = () => {
    const screenWidth = document.body.offsetWidth;

    nextTick(() => {
      if (
        (machineStateVoList.value.length > 8 && screenWidth > 1440) ||
        (machineStateVoList.value.length > 6 && screenWidth <= 1440)
      ) {
        const slide = machineStateVoList.value.length > 8 && screenWidth > 1440 ? 8 : 6;
        maxSlide.value = Math.floor(machineStateVoList.value.length / slide);
        swiper.value = new Swiper('.swiper-container', {
          pagination: '.swiper-pagination',
          slidesPerView: slides.value,
          slidesPerGroup: slides.value,
          initialSlide: initialSlide.value,
          slidesPerColumn: 2,
          spaceBetween: 56,
          autoplay: 15000,
          speed: 900,
          autoplayDisableOnInteraction: false,
          observer: true,
          observeParents: true,
          slidesPerColumnFill: 'row',
          onlyExternal: true,
          paginationClickable: true,
        });
      } else {
        maxSlide.value = 0;
        swiper.value = new Swiper('.swiper-container', {
          pagination: '.swiper-pagination',
          slidesPerView: slides.value,
          slidesPerGroup: slides.value,
          slidesPerColumn: 2,
          spaceBetween: 56,
          autoplayDisableOnInteraction: false,
          observer: true,
          observeParents: true,
          slidesPerColumnFill: 'row',
          onlyExternal: true,
          paginationClickable: true,
        });
      }
    });
  };

  const resizeFun = () => {
    const screenWidth = document.body.offsetWidth;

    if (screenWidth <= 1440 && flag.value) {
      flag.value = false;
      slides.value = 3;
      getData();
      firstSwiper.value = true;
      return;
    }
    if (screenWidth > 1440 && !flag.value) {
      flag.value = true;
      slides.value = 4;
      getData();
      firstSwiper.value = true;
      return;
    }
  };

  // Lifecycle Hooks
  onMounted(() => {
    loadLangFn(lang);
    firstSwiper.value = true;
    initialSlide.value = 0;
    maxSlide.value = 0;

    timer.value = setInterval(() => {
      if (swiper.value) {
        if (swiper.value.snapIndex === maxSlide.value) {
          swiper.value.slideTo(0, 0, false);
        } else {
          swiper.value.slideNext();
        }
      }
      getData();
    }, 15000);

    window.addEventListener('resize', resizeFun);
  });

  onBeforeUnmount(() => {
    clearInterval(timer.value);

    time && clearTimeout(time);
    window.removeEventListener('resize', resizeFun);
  });

  const initFn = () => {
    if (swiperNext.value) {
      firstSwiper.value = true;
      getData();
    }
  };

  const collapsed = ref(false);

  settingStore.$subscribe((_mutation, state) => {
    if (state.collapsed !== collapsed.value) {
      initFn();

      collapsed.value = state.collapsed;
    }
  });

  initFn();
</script>

<style scoped lang="less">
  .home {
    .swiper-wrapper {
      height: auto;
    }
    .n-spin-container {
      width: 100%;
    }
    i {
      font-style: normal;
    }
    .iconfont {
      display: block;
      margin: 4px auto;
    }
    .empty-icon {
      display: block;
      padding: 100px 0 16px;
      cursor: pointer;
      font-size: 276px;
      margin: 0 auto;
      // vertical-align: middle;
    }
    .noeq {
      text-align: center;
    }
    &-item {
      background: #fff;
      border-radius: 2px;
    }
    &-title {
      font-size: 18px;
      line-height: 50px;
      font-family: Source Han Sans CN, Source Han Sans CN-Regular;
      font-weight: 400;
      text-align: center;
      color: #222;
      border-bottom: 1px solid #e8e8e8;
    }
    .today {
      margin-bottom: 16px;
      &-body {
        padding: 16px 4.23% 0;
        display: flex;
        justify-content: space-between;
        flex-wrap: wrap;
      }
      .n-col {
        width: 27%;
        &:nth-child(3n-1) {
          margin: 0 9.5%;
        }
        @media screen and (max-width: 1024px) {
          width: 42.5%;
          &:nth-child(3n-1) {
            margin: 0;
          }
          &:nth-child(2n + 1) {
            margin-right: 15%;
          }
        }
        @media screen and (max-width: 768px) {
          width: 100%;
          &:nth-child(2n + 1) {
            margin-right: 0;
          }
        }

        &:nth-of-type(1) {
          .today-item {
            background: linear-gradient(90deg, #3d62e3, #3057db);
            box-shadow: 0px 3px 6px 0px rgba(60, 98, 227, 0.5);
          }
        }
        &:nth-of-type(2) {
          .today-item {
            background: linear-gradient(90deg, #00a5f1, #01b9f0);
            box-shadow: 0px 3px 6px 0px rgba(0, 165, 241, 0.5);
          }
        }
        &:nth-of-type(3) {
          .today-item {
            background: linear-gradient(90deg, #ff9655, #ff8355);
            box-shadow: 0px 3px 6px 0px rgba(255, 150, 85, 0.5);
          }
        }
        &:nth-of-type(4) {
          .today-item {
            background: linear-gradient(90deg, #5795ff, #449bf2);
            box-shadow: 0px 3px 6px 0px rgba(87, 149, 255, 0.5);
          }
        }
        &:nth-of-type(5) {
          .today-item {
            background: linear-gradient(90deg, #0cd7e8, #0babd1);
            box-shadow: 0px 3px 6px 0px rgba(12, 215, 232, 0.5);
          }
        }
        &:nth-of-type(6) {
          .today-item {
            background: linear-gradient(90deg, #ffb755, #f29b25);
            box-shadow: 0px 3px 6px 0px rgba(255, 183, 85, 0.5);
          }
        }
      }
      &-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        text-align: center;
        height: 100px;
        // border: 1px solid #e3e3e3;
        border-radius: 12px;
        box-shadow: 0px 3px 6px 0px rgba(0, 0, 0, 0.1);
        margin-bottom: 16px;
        position: relative;
        &-icon {
          position: absolute;
          right: 0;
          top: 0;
          .svg-icon {
            width: 80px;
            height: 100px;
          }
          img {
            display: block;
            width: 80px;
            height: 100px;
          }
        }
        &-desc {
          width: 100%;
          text-align: center;
          p {
            font-size: 18px;
            line-height: 1;
            font-family: Source Han Sans CN, Source Han Sans CN-Regular;
            font-weight: 400;
            color: #fff;
            &.num {
              font-size: 34px;
              font-family: Source Han Sans CN, Source Han Sans CN-Medium;
              font-weight: 500;
              margin-bottom: 12px;
              color: #fff;
            }
            &.numColor {
              color: #fff;
            }
            @media screen and (max-width: 1200px) {
              font-size: 14px;
              &.num {
                font-size: 24px;
              }
            }
          }
        }
      }
    }
    .equipment {
      &-body {
        padding: 24px 4.23% 20px;
        min-height: 479px;
        box-sizing: border-box;
        ::v-deep .swiper-pagination {
          bottom: 0;
          position: static;
          height: 10px;
          &-bullet {
            width: 10px;
            height: 10px;
            vertical-align: top;
          }
          &-bullet-active {
            background: #207af6;
          }
        }
      }
      &-item {
        box-sizing: border-box;
        position: relative;
        height: 204px;
        padding: 0px 0px 14px;
        background: #ffffff;
        // border: 1px solid #e2e2e2;
        border-radius: 12px;
        box-shadow: 0px 3px 6px 0px rgba(0, 0, 0, 0.1);
        margin-bottom: 24px;
        margin-top: 0 !important;
        overflow: hidden;
        &::after {
          content: '';
          position: absolute;
          height: 8px;
          width: 100%;
          left: 0;
          bottom: 0;
        }
        &-desc {
          padding: 0px 20px;
        }
        @media screen and (max-width: 1440px) {
          padding: 0px 0 14px;
          &-desc {
            padding: 0px 15px;
          }
        }
        @media screen and (max-width: 1200px) {
          padding: 0px 0 14px;
          &-desc {
            padding: 0px 10px;
          }
        }
        &-title {
          font-size: 20px;
          line-height: 1;
          font-family: Source Han Sans CN, Source Han Sans CN-Medium;
          font-weight: 500;
          color: #222;
          margin-bottom: 30px;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
          @media screen and (max-width: 1200px) {
            font-size: 16px;
          }
        }
        &-desc {
          padding-top: 8px;
          p {
            display: flex;
            justify-content: space-between;
            align-items: flex-end;
            margin-bottom: 8px;
            font-size: 16px;
            line-height: 1;
            font-family: Source Han Sans CN, Source Han Sans CN-Regular;
            color: #999;
            span {
              display: block;
            }
            &:first-child {
              color: #222;
              span:last-child {
                color: #207af6;
              }
            }
            &:last-child {
              color: #207af6;
              span:last-child {
                color: #207af6;
              }
            }
            span:last-child {
              font-size: 20px;
              color: #666;
            }
            i {
              font-size: 14px;
              width: 36px;
              display: inline-block;
              color: #666;
            }
            @media screen and (max-width: 1200px) {
              font-size: 14px;
              i {
                font-size: 12px;
              }
            }
          }
        }
        &-status {
          position: absolute;
          right: 0;
          top: 0;
          text-align: center;
          color: #fff;
          width: 74px;
          height: 26px;
          line-height: 26px;
          background: #52c570;
          border-radius: 0px 2px 0px 2px;
          font-size: 14px;
          font-family: Source Han Sans CN, Source Han Sans CN-Regular;
          font-weight: 400;
          @media screen and (max-width: 1200px) {
            width: 60px;
          }
        }
      }
      &-item1 {
        background: #f6fff9;
        &::after {
          background: #52c570;
        }
        .equipment-head-i {
          color: #52c570;
        }
      }
      &-item2 {
        background: #fcf8f1;
        &::after {
          background: #ffb843;
        }
        .equipment-head-i {
          color: #ffb843;
        }
      }
      &-itemnull,
      &-item0 {
        background: #f7f8fa;
        &::after {
          background: #6a7784;
        }
        .equipment-head-i {
          color: #6a7784;
        }
      }
      &-head {
        display: flex;
        padding: 0 20px;
        border-bottom: 1px solid #e8e8e8;
        line-height: 44px;
        @media screen and (max-width: 1440px) {
          padding: 0px 15px;
        }
        @media screen and (max-width: 1200px) {
          padding: 0px 10px;
        }
        &-i {
          text-align: center;
          padding-top: 5px;
          i {
            display: block;
            font-size: 16px;
            line-height: 20px;
          }
          span {
            display: block;
            font-size: 10px;
            line-height: 10px;
            text-align: center;
            white-space: nowrap;
          }
        }
        &-tit {
          flex: 1;
          margin-left: 10px;
          font-size: 18px;
          font-family: Source Han Sans CN, Source Han Sans CN-Medium;
          font-weight: 500;
          text-align: left;
          color: #222222;
          line-height: 44px;
        }
        &-num {
          font-size: 14px;
          font-family: Source Han Sans CN, Source Han Sans CN-Regular;
          font-weight: 400;
          text-align: right;
          color: #207af6;
          line-height: 44px;
          white-space: nowrap;
          i {
            font-style: normal;
            display: inline-block;
            width: 19px;
            height: 19px;
            background: #ff0000;
            border-radius: 50%;
            font-size: 10px;
            line-height: 18px;
            color: #fff;
            text-align: center;
            margin-left: 4px;
            position: relative;
            top: -1px;
          }
        }
      }
    }
    &-copyright {
      font-size: 13px;
      font-family: SourceHanSansCN-Regular;
      color: #999;
      line-height: 20px;
      padding: 19px 0 3px;
      display: flex;
      justify-content: center;
    }
  }
  .EnApp {
    .equipment-item-desc {
      p {
        i {
          width: 52px;
        }
      }
    }
  }
</style>
